﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiscoveryLogic.Analytics.DataObjects;

namespace DiscoveryLogic.Analytics.Modules.Entities
{
    /// <summary>
    /// Interaction logic for DocListView.xaml
    /// </summary>
    public partial class DocListView : UserControl
    {
        public delegate void OnDocSelected(Document doc);
        public event OnDocSelected DocSelected;

        private DocListPresenter _Presenter;

        public DocListView()
        {
            InitializeComponent();
        }

        public DocListView(DocListPresenter presneter)
			: this()
		{
			_Presenter = presneter;
			_Presenter.View = this;
		}

        public List<Document> Docs
        {
            get { return this.DataContext as List<Document>; }
            set
            {
                this.DataContext = value;
                if (value == null || value.Count == 0)
                {
                    this.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    this.Visibility = System.Windows.Visibility.Visible;
                }
            }
        }

        public void SubscribeEventHandler(Document doc)
        {
            this._Presenter.RegisterDocSelectionChangedEventHandler(doc);
        }

        private void lvDocs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 1)
            {
                Document doc = e.AddedItems[0] as Document;
                if (this.DocSelected != null)
                    this.DocSelected(doc);
            }
        }

        public void SelectDoc(Document doc)
        {
            if (this.Docs != null && doc != null)
            {
                for (int i = 0; i < this.Docs.Count; i++)
                {
                    if (this.Docs[i].DocID == doc.DocID)
                    {
                        this.lvDocs.SelectedIndex = i;
                        break;
                    }
                }
            }
        }
    }
}
